<?php
/**
* Export Controller
*/
class ExportController extends AppController {
   	var $name = 'Export';
//	var $components = array('UserAuth');
	var $uses = array('Project', 'Batch');
	
	public function beforeFilter()
	{
		$this->project_id = $this->params['projectId'];
		$this->project_name = $this->params['projectName'];		
		$this->set('project_id', $this->project_id);
		$this->set('project_name', $this->project_name);
		$this->base_url = $this->base;
		$this->set('base_url', $this->base);
		$this->user = $this->Session->read('User');
		
		$db =&ConnectionManager::getDataSource('default');
		$db->fullDebug = false;
	}

	/**
	 * Return the Xls for the "In Process" element of the dashboard. (used on smile people board)
	 *
	 * @author Kévin Labesse
	 **/	
	public function xlsTickets()
	{
		$results = $this->Project->query('SELECT b.id, b.name, m.name, m.deadline FROM batches AS b LEFT JOIN milestones AS m ON m.batch_id = b.id WHERE b.project_id='.$this->project_id);
		
		header("Content-Type: application/force-download");
		header("Content-Disposition: attachment; filename=\"webx_tickets.xls\"");

		echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"'."\n";
		echo 'xmlns:x="urn:schemas-microsoft-com:office:excel"'."\n";
		echo 'xmlns="www.w3.org/TR/REC-html40&...'."\n";
		echo '<head>'."\n";
			echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'."\n";
			echo '<meta name="ProgId" content="Excel.Sheet" />'."\n";
			echo '<meta name="Generator" content="Microsoft Excel 9" />'."\n";
			echo '<style>'."\n";
			echo '<!--//'."\n";
			echo 'td.xleuro {'."\n";
			echo 'mso-style-parent:style0;'."\n";
			echo 'mso-number-format:"\#\,\#\#0\.00\\ \0022\20AC\0022";'."\n";
			echo '}'."\n";
			echo '//-->'."\n";
			echo '</style>'."\n";
		echo '</head>'."\n";
		echo '<body>'."\n";
		echo '<table x:str="true" border="1" cellpadding="0" cellspacing="0">'."\n";
			echo '<tr>'."\n";
				echo '<th>Nom</th>'."\n";
				echo '<th>Jalon</th>'."\n";
				echo '<th>Date</th>'."\n";
			echo '</tr>'."\n";
			
		foreach($results as $result):
			echo '<tr>'."\n";
			echo '<td>'.$result['b']['name'].'</td>'."\n";
			echo '<td>'.$result['m']['name'].'</td>'."\n";
			echo '<td>'.$result['m']['deadline'].'</td>'."\n";
			echo '</tr>'."\n";
			
			$tickets = $this->Project->query('SELECT t.id, t.name, t.kind, t.status FROM tickets AS t WHERE t.batch_id='.$result['b']['id']);
			
			echo '<tr><td></td>'."\n";
			echo '<td colspan="2">'."\n";
				echo '<table x:str="true" border="1" cellpadding="0" cellspacing="0">'."\n";
					echo '<tr>'."\n";
						echo '<th>Nom</th>'."\n";
						echo '<th>Type</th>'."\n";
						echo '<th>Etat</th>'."\n";
						echo '<th>Affectation</th>'."\n";
					echo '</tr>'."\n";
					
				foreach($tickets as $ticket):
				
				$users = $this->Project->query('SELECT u.firstname, u.lastname FROM ticket_users AS tu LEFT JOIN users AS u ON u.id = tu.user_id WHERE tu.ticket_id='.$ticket['t']['id']);
				
				$usr = "";
				for($i = 0; $i < sizeof($users); $i++){
					$usr .= $users[$i]['u']['firstname']." ".$users[$i]['u']['lastname'];
					if ($i < (sizeof($users) - 1))
						$usr .= "; ";
				}
				
					echo '<tr>'."\n";
						echo '<td>'.$ticket['t']['name'].'</td>'."\n";
						echo '<td>'.$ticket['t']['kind'].'</td>'."\n";
						echo '<td>'.$ticket['t']['status'].'</td>'."\n";
						echo '<td>'.$usr.'</td>'."\n";						
					echo '</tr>'."\n"; 
				endforeach;		
					
				echo '</table>'."\n";
			echo '</td></tr>'."\n";
			
		endforeach;	

		echo '</table>'."\n";
		echo '</body>'."\n";
		echo '</html>';
	}

	/**
	 * Return the XML for the "In Process" element of the dashboard. (used on smile people board)
	 *
	 * @author Kévin Labesse
	 **/	
	public function xmlTickets()
	{
		$results = $this->Project->query('SELECT b.id, b.name, m.name, m.deadline FROM batches AS b LEFT JOIN milestones AS m ON m.batch_id = b.id WHERE b.project_id='.$this->project_id);

		header("Content-Type: application/force-download");
		header("Content-Disposition: attachment; filename=\"webx_tickets.xml\"");

		echo "<"."?xml version=\"1.0\" encoding=\"UTF-8\"?".">\n";		
		foreach($results as $result):
			echo "<batch>\n";
				echo "\t<name>".$result['b']['name']."</name>\n";
				echo "\t<milestone>".$result['m']['name']."</milestone>\n";
				echo "\t<deadline>".$result['m']['deadline']."</deadline>\n";
				
				$tickets = $this->Project->query('SELECT t.name, t.kind, t.status, u.firstname, u.lastname FROM tickets AS t LEFT JOIN ticket_users AS tu ON tu.ticket_id = t.id LEFT JOIN users AS u ON u.id = tu.user_id WHERE t.batch_id='.$result['b']['id']);
				
				foreach($tickets as $ticket):
					echo "\t<ticket>\n";
					echo "\t\t<name>".$ticket['t']['name']."</name>\n";
					echo "\t\t<kind>".$ticket['t']['kind']."</kind>\n";
					echo "\t\t<state>".$ticket['t']['status']."</state>\n";
					echo "\t\t<affected>".$ticket['u']['firstname']." ".$ticket['u']['lastname']."</affected>\n";
					echo "\t</ticket>\n";
				endforeach;
				
			echo "</batch>\n\n";
		endforeach;
		
		exit();
	}

	/**
	 * Return the Rss for the "In Process" element of the dashboard. (used on smile people board)
	 *
	 * @author Kévin Labesse
	 **/	
	public function rssAllTickets()
	{
		$results = $this->Project->query("SELECT t.id, t.name AS tname, t.description, p.id AS pid, p.name AS pname, r.response, u.lastname, u.firstname, r.date FROM tickets AS t LEFT JOIN batches AS b ON t.batch_id = b.id LEFT JOIN projects AS p ON b.project_id = p.id LEFT JOIN replies AS r ON r.ticket_id=t.id LEFT JOIN users AS u ON r.user_id=u.id WHERE p.id=".$this->project_id." ORDER BY r.date DESC");
		
		echo '<'.'?xml version="1.0" encoding="UTF-8"?'.'><rss version="2.0"><channel>';
		
		echo '<title>'.$this->project_name.'</title>';
		echo '<link>http://www.smile.fr</link>';
		echo '<description>Les derniers tickets</description>'."\n\n";
		
		foreach ($results as $result)
		{	
			$titre = $result['p']['pname']." - ".$result['t']['tname'];
			$titre = stripslashes(trim($titre));
			$titre = str_replace("&nbsp;",' ',$titre);
			$titre = str_replace("<BR>",'<BR/>',$titre);
			$titre = str_replace("&",'&amp;',$titre);
			$titre = str_replace("<","&lt;",$titre);
			$titre = str_replace(">","&gt;",$titre);
			$news = $result['u']['lastname'].' '.$result['u']['firstname'].': ';
			$news .= stripslashes(trim($result['r']['response']));
			$news = str_replace("&nbsp;",'',$news);
			$news = str_replace("<BR>",'<BR/>',$news);
			$news = str_replace("&",'&amp;',$news);
			$news = str_replace("<","&lt;",$news);
			$news = str_replace(">","&gt;",$news);
			
			echo '<item>'."\n";
			echo "\t".'<title>'.$titre.'</title>'."\n";
			echo "\t".'<link>'.$this->base_url.'/'.$result['p']['pname'].'/'.$result['p']['pid'].'/tickets/view/'.$result['t']['id'].'</link>'."\n";
			echo "\t".'<pubDate>'.$result['r']['date'].' GMT+1</pubDate>'."\n";
			echo "\t".'<guid>http://localhost'.$this->base_url.'/'.$result['p']['pname'].'/'.$result['p']['pid'].'/tickets/</guid>'."\n";
			echo "\t".'<comments>'.$this->base_url.'/'.$result['p']['pname'].'/'.$result['p']['pid'].'/tickets/</comments>'."\n";
			echo "\t".'<description>';
			
			echo $news; 
			
			echo '</description>'."\n".'</item>'."\n\n";
		}
		
		echo '</channel></rss>';
		
		exit();
	}	

	/**
	 * Return the information for the "No linked ticket" element of the dashboard. (used on smile people board)
	 *
	 * @return array
	 * @author Arnaud Vuillaume
	 **/
	private function get_no_linked_tickets($in_process) {
		$ret = array();
		foreach($in_process as $batch) {
			foreach($batch['Ticket'] as $ticket) {
				$affect = '';
				foreach($ticket['UserTicket'] as $user) {
					if ($affect != '')
						$affect .= ', ';
					$affect .= $user['firstname'].' '.$user['lastname'];
				}
				if ($affect == '' && $ticket['description'] != '')
					$ret[] = array('id' => $ticket['id'], 'name' => $ticket['name'], 'kind' => $ticket['kind'], 'description' => $ticket['description'], 'status' => $ticket['status'], 'milestone' => $ticket['Milestone']?$ticket['Milestone']['name']:'', 'deadline' => $ticket['Milestone']?$ticket['Milestone']['deadline']:'');									
			}
		}
		return $ret;
	}

	/**
	 * Return the Rss for the "No linked Ticket" element of the dashboard. (used on smile people board)
	 *
	 * @author Kévin Labesse
	 **/	
	public function rssNATickets()
	{
		$this->Batch->unbindModel(array('belongsTo' => array('Project')));
		$this->Batch->Ticket->unbindModel(array(
			'belongsTo' => array('User', 'Batch'),
			'hasMany' => array('Reply', 'Document', 'Funding')));
		$ret = $this->Batch->findAll(array('project_id' => $this->project_id, 'finished' => 0), NULL, NULL, NULL, 1, 3);
		$nat = $this->get_no_linked_tickets($ret);
		//pr($nat);	
		
		echo '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<rss version="2.0">'."\n".'<channel>'."\n";
		
		echo '<title>'.$this->project_name.'</title>'."\n";
		echo '<link>http://www.smile.fr</link>'."\n";
		echo '<description>Les tickets non affectes</description>'."\n\n";
		
		foreach ($nat as $result)
		{	
			$titre = $result['milestone'].' - '.$result['name'];
			$titre = stripslashes(trim($titre));
			$titre = str_replace("&nbsp;",'',$titre);
			$news = stripslashes(trim($result['description']));
			$news = str_replace("<BR>",'<BR/>',$news);
			$news = str_replace("&",'&amp;',$news);
			$news = str_replace("<","&lt;",$news);
			$news = str_replace(">","&gt;",$news);
			$news = str_replace("", "e", $news);
			
			echo '<item>'."\n";
			echo '<title>'.$titre.'</title>'."\n";
			echo '<link>'.$this->base_url.'/'.$this->project_name.'/'.$this->project_id.'/tickets/view/'.$result['id'].'</link>'."\n";
			echo '<description>';
			
			echo  $news; 
			
			echo '</description>'."\n".'</item>'."\n\n";
		}
		
		echo '</channel>'."\n".'</rss>';
		exit();
	}
	
	/**
	 * Return the Xls for the "No linked tickets" element of the dashboard. (used on smile people board)
	 *
	 * @author Kévin Labesse
	 **/	
	public function xlsNATickets()
	{
		$this->Batch->unbindModel(array('belongsTo' => array('Project')));
		$this->Batch->Ticket->unbindModel(array(
			'belongsTo' => array('User', 'Batch'),
			'hasMany' => array('Reply', 'Document', 'Funding')));
		$ret = $this->Batch->findAll(array('project_id' => $this->project_id, 'finished' => 0), NULL, NULL, NULL, 1, 3);
		$nat = $this->get_no_linked_tickets($ret);
		
		header("Content-Type: application/force-download");
		header("Content-Disposition: attachment; filename=\"webx_tickets.xls\"");

		echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"'."\n";
		echo 'xmlns:x="urn:schemas-microsoft-com:office:excel"'."\n";
		echo 'xmlns="www.w3.org/TR/REC-html40&...'."\n";
		echo '<head>'."\n";
			echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'."\n";
			echo '<meta name="ProgId" content="Excel.Sheet" />'."\n";
			echo '<meta name="Generator" content="Microsoft Excel 9" />'."\n";
			echo '<style>'."\n";
			echo '<!--//'."\n";
			echo 'td.xleuro {'."\n";
			echo 'mso-style-parent:style0;'."\n";
			echo 'mso-number-format:"\#\,\#\#0\.00\\ \0022\20AC\0022";'."\n";
			echo '}'."\n";
			echo '//-->'."\n";
			echo '</style>'."\n";
		echo '</head>'."\n";
		echo '<body>'."\n";
		echo '<table x:str="true" border="1" cellpadding="0" cellspacing="0">'."\n";
			echo '<tr>'."\n";
				echo '<th>Nom</th>'."\n";
				echo '<th>Jalon</th>'."\n";
				echo '<th>Date</th>'."\n";
			echo '</tr>'."\n";
			
		foreach($nat as $result):
			echo '<tr>'."\n";
			echo '<td>'.$result['name'].'</td>'."\n";
			echo '<td>'.$result['milestone'].'</td>'."\n";
			echo '<td>'.$result['deadline'].'</td>'."\n";
			echo '</tr>'."\n";
			
			$tickets = $this->Project->query('SELECT t.id, t.name, t.kind, t.status FROM tickets AS t WHERE t.batch_id='.$result['id']);
			
			echo '<tr><td></td>'."\n";
			echo '<td colspan="2">'."\n";
				echo '<table x:str="true" border="1" cellpadding="0" cellspacing="0">'."\n";
					echo '<tr>'."\n";
						echo '<th>Nom</th>'."\n";
						echo '<th>Type</th>'."\n";
						echo '<th>Etat</th>'."\n";
						echo '<th>Affectation</th>'."\n";
					echo '</tr>'."\n";
					
				foreach($tickets as $ticket):
				
				$users = $this->Project->query('SELECT u.firstname, u.lastname FROM ticket_users AS tu LEFT JOIN users AS u ON u.id = tu.user_id WHERE tu.ticket_id='.$ticket['t']['id']);
				
				$usr = "";
				for($i = 0; $i < sizeof($users); $i++){
					$usr .= $users[$i]['u']['firstname']." ".$users[$i]['u']['lastname'];
					if ($i < (sizeof($users) - 1))
						$usr .= "; ";
				}
				
					echo '<tr>'."\n";
						echo '<td>'.$ticket['t']['name'].'</td>'."\n";
						echo '<td>'.$ticket['t']['kind'].'</td>'."\n";
						echo '<td>'.$ticket['t']['status'].'</td>'."\n";
						echo '<td>'.$usr.'</td>'."\n";						
					echo '</tr>'."\n"; 
				endforeach;		
					
				echo '</table>'."\n";
			echo '</td></tr>'."\n";
			
		endforeach;	

		echo '</table>'."\n";
		echo '</body>'."\n";
		echo '</html>';
	}	

	/**
	 * Return the information for the "Waiting Client Reply" element of the dashbord. (used on client team leader bord)
	 *
	 * @return array
	 * @author Arnaud Vuillaume
	 **/
	private function get_waiting_client_reply($in_process) {
		$ret = array();
		foreach($in_process as $batch) {
			foreach($batch['Ticket'] as $ticket) {
				$affect = '';
				foreach($ticket['UserTicket'] as $user) {
					if ($affect != '')
						$affect .= ', ';
					$affect .= $user['firstname'].' '.$user['lastname'];
				}
				if ($ticket['status'] == 'idle client' && $ticket['description'] != '')
					$ret[] = array('id' => $ticket['id'], 'name' => $ticket['name'], 'kind' => $ticket['kind'], 			'description' => $ticket['description'], 'status' => $ticket['status'], 'affect' => $affect, 'milestone' => $ticket['Milestone']?$ticket['Milestone']['name']:'', 'deadline' => $ticket['Milestone']?$ticket['Milestone']['deadline']:'');
			}
		}
		return $ret;
	}
		
	/**
	 * Return the Rss for the "Waiting Client Reply" element of the dashboard.
	 *
	 * @author Kévin Labesse
	 **/	
	public function rssWCRTickets()
	{
		$this->Batch->unbindModel(array('belongsTo' => array('Project')));
		$this->Batch->Ticket->unbindModel(array(
			'belongsTo' => array('User', 'Batch'),
			'hasMany' => array('Reply', 'Document', 'Funding')));
		$ret = $this->Batch->findAll(array('project_id' => $this->project_id, 'finished' => 0), NULL, NULL, NULL, 1, 3);
		$wcrt =  $this->get_waiting_client_reply($ret);

		echo '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<rss version="2.0">'."\n".'<channel>'."\n";
		
		echo '<title>'.$this->project_name.'</title>'."\n";
		echo '<link>http://www.smile.fr</link>'."\n";
		echo '<description>Les tickets en attente de réponse client</description>'."\n\n";
		
		foreach ($wcrt as $result)
		{	
			$titre = $result['milestone'].' - '.$result['name'];
			$titre = stripslashes(trim($titre));
			$titre = str_replace("&nbsp;",'',$titre);
			$news = stripslashes(trim($result['description']));
			$news = str_replace("<BR>",'<BR/>',$news);
			$news = str_replace("&",'&amp;',$news);
			$news = str_replace("<","&lt;",$news);
			$news = str_replace(">","&gt;",$news);
			$news = str_replace("", "e", $news);
			
			echo '<item>'."\n";
			echo '<title>'.$titre.'</title>'."\n";
			echo '<link>'.$this->base_url.'/'.$this->project_name.'/'.$this->project_id.'/tickets/view/'.$result['id'].'</link>'."\n";
			echo '<description>';
			
			echo  $news; 
			
			echo '</description>'."\n".'</item>'."\n\n";
		}
		
		echo '</channel>'."\n".'</rss>';
		exit();		
	}
	
	/**
	 * Return the Xls for the "No linked tickets" element of the dashboard. (used on smile people board)
	 *
	 * @author Kévin Labesse
	 **/	
	public function xlsWCRTickets()
	{
		$this->Batch->unbindModel(array('belongsTo' => array('Project')));
		$this->Batch->Ticket->unbindModel(array(
			'belongsTo' => array('User', 'Batch'),
			'hasMany' => array('Reply', 'Document', 'Funding')));
		$ret = $this->Batch->findAll(array('project_id' => $this->project_id, 'finished' => 0), NULL, NULL, NULL, 1, 3);
		$wcrt =  $this->get_waiting_client_reply($ret);
		
		header("Content-Type: application/force-download");
		header("Content-Disposition: attachment; filename=\"webx_tickets.xls\"");

		echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"'."\n";
		echo 'xmlns:x="urn:schemas-microsoft-com:office:excel"'."\n";
		echo 'xmlns="www.w3.org/TR/REC-html40&...'."\n";
		echo '<head>'."\n";
			echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'."\n";
			echo '<meta name="ProgId" content="Excel.Sheet" />'."\n";
			echo '<meta name="Generator" content="Microsoft Excel 9" />'."\n";
			echo '<style>'."\n";
			echo '<!--//'."\n";
			echo 'td.xleuro {'."\n";
			echo 'mso-style-parent:style0;'."\n";
			echo 'mso-number-format:"\#\,\#\#0\.00\\ \0022\20AC\0022";'."\n";
			echo '}'."\n";
			echo '//-->'."\n";
			echo '</style>'."\n";
		echo '</head>'."\n";
		echo '<body>'."\n";
		echo '<table x:str="true" border="1" cellpadding="0" cellspacing="0">'."\n";
			echo '<tr>'."\n";
				echo '<th>Nom</th>'."\n";
				echo '<th>Jalon</th>'."\n";
				echo '<th>Date</th>'."\n";
			echo '</tr>'."\n";
			
		foreach($wcrt as $result):
			echo '<tr>'."\n";
			echo '<td>'.$result['name'].'</td>'."\n";
			echo '<td>'.$result['milestone'].'</td>'."\n";
			echo '<td>'.$result['deadline'].'</td>'."\n";
			echo '</tr>'."\n";
			
			$tickets = $this->Project->query('SELECT t.id, t.name, t.kind, t.status FROM tickets AS t WHERE t.batch_id='.$result['id']);
			
			echo '<tr><td></td>'."\n";
			echo '<td colspan="2">'."\n";
				echo '<table x:str="true" border="1" cellpadding="0" cellspacing="0">'."\n";
					echo '<tr>'."\n";
						echo '<th>Nom</th>'."\n";
						echo '<th>Type</th>'."\n";
						echo '<th>Etat</th>'."\n";
						echo '<th>Affectation</th>'."\n";
					echo '</tr>'."\n";
					
				foreach($tickets as $ticket):
				
				$users = $this->Project->query('SELECT u.firstname, u.lastname FROM ticket_users AS tu LEFT JOIN users AS u ON u.id = tu.user_id WHERE tu.ticket_id='.$ticket['t']['id']);
				
				$usr = "";
				for($i = 0; $i < sizeof($users); $i++){
					$usr .= $users[$i]['u']['firstname']." ".$users[$i]['u']['lastname'];
					if ($i < (sizeof($users) - 1))
						$usr .= "; ";
				}
				
					echo '<tr>'."\n";
						echo '<td>'.$ticket['t']['name'].'</td>'."\n";
						echo '<td>'.$ticket['t']['kind'].'</td>'."\n";
						echo '<td>'.$ticket['t']['status'].'</td>'."\n";
						echo '<td>'.$usr.'</td>'."\n";						
					echo '</tr>'."\n"; 
				endforeach;		
					
				echo '</table>'."\n";
			echo '</td></tr>'."\n";
			
		endforeach;	

		echo '</table>'."\n";
		echo '</body>'."\n";
		echo '</html>';
	}	
	
	/**
	 * Return the information for the "Idle task" element of the dashbord. (used on smile team leader bord)
	 *
	 * @return array
	 * @author Arnaud Vuillaume
	 **/
	private function get_idle($in_process, $user_id) {
		$ret = array();
		foreach($in_process as $batch) {
			foreach($batch['Ticket'] as $ticket) {
				$affect = '';
				$dev = false;
				foreach($ticket['UserTicket'] as $user) {
					if ($user['id'] == $user_id)
						$dev = true;
					if ($affect != '')
						$affect .= ', ';
					$affect .= $user['firstname'].' '.$user['lastname'];
				}
				if ($dev && $ticket['status'] == 'idle' && $ticket['description'] != '')
					$ret[] = array('id' => $ticket['id'], 'name' => $ticket['name'], 'kind' => $ticket['kind'], 'description' => $ticket['description'], 'status' => $ticket['status'], 'affect' => $affect, 'milestone' => $ticket['Milestone']?$ticket['Milestone']['name']:'', 'deadline' => $ticket['Milestone']?$ticket['Milestone']['deadline']:'');
			}
		}
		return $ret;
	}
	
	/**
	 * Return the Rss for the "Idle" element of the dashboard.
	 *
	 * @author Kévin Labesse
	 **/	
	public function rssIdleTickets()
	{
		$this->Batch->unbindModel(array('belongsTo' => array('Project')));
		$this->Batch->Ticket->unbindModel(array(
			'belongsTo' => array('User', 'Batch'),
			'hasMany' => array('Reply', 'Document', 'Funding')));
		$ret = $this->Batch->findAll(array('project_id' => $this->project_id, 'finished' => 0), NULL, NULL, NULL, 1, 3);
		$idle =  $this->get_idle($ret, $this->user['User']['id']);

		echo '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<rss version="2.0">'."\n".'<channel>'."\n";
		
		echo '<title>'.$this->project_name.'</title>'."\n";
		echo '<link>http://www.smile.fr</link>'."\n";
		echo '<description>Les réponses à mes tickets</description>'."\n";
		
		foreach ($idle as $result)
		{	
			$titre = $result['milestone'].' - '.$result['name'];
			$titre = stripslashes(trim($titre));
			$titre = str_replace("&nbsp;",'',$titre);
			$news = stripslashes(trim($result['description']));
			$news = str_replace("<BR>",'<BR/>',$news);
			$news = str_replace("&",'&amp;',$news);
			$news = str_replace("<","&lt;",$news);
			$news = str_replace(">","&gt;",$news);
			$news = str_replace("", "e", $news);
			
			echo '<item>'."\n";
			echo '<title>'.$titre.'</title>'."\n";
			echo '<link>'.$this->base_url.'/'.$this->project_name.'/'.$this->project_id.'/tickets/view/'.$result['id'].'</link>'."\n";
			echo '<description>';
			
			echo  $news; 
			
			echo '</description>'."\n".'</item>'."\n";
		}
		
		echo '</channel>'."\n".'</rss>';
		exit();		
	}
	
	/**
	 * Return the Xls for the "Idle" element of the dashboard. (used on smile people board)
	 *
	 * @author Kévin Labesse
	 **/	
	public function xlsIdleTickets()
	{
		$this->Batch->unbindModel(array('belongsTo' => array('Project')));
		$this->Batch->Ticket->unbindModel(array(
			'belongsTo' => array('User', 'Batch'),
			'hasMany' => array('Reply', 'Document', 'Funding')));
		$ret = $this->Batch->findAll(array('project_id' => $this->project_id, 'finished' => 0), NULL, NULL, NULL, 1, 3);
		$idle =  $this->get_idle($ret, $this->user['User']['id']);
		
		header("Content-Type: application/force-download");
		header("Content-Disposition: attachment; filename=\"webx_tickets.xls\"");

		echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"'."\n";
		echo 'xmlns:x="urn:schemas-microsoft-com:office:excel"'."\n";
		echo 'xmlns="www.w3.org/TR/REC-html40&...'."\n";
		echo '<head>'."\n";
			echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'."\n";
			echo '<meta name="ProgId" content="Excel.Sheet" />'."\n";
			echo '<meta name="Generator" content="Microsoft Excel 9" />'."\n";
			echo '<style>'."\n";
			echo '<!--//'."\n";
			echo 'td.xleuro {'."\n";
			echo 'mso-style-parent:style0;'."\n";
			echo 'mso-number-format:"\#\,\#\#0\.00\\ \0022\20AC\0022";'."\n";
			echo '}'."\n";
			echo '//-->'."\n";
			echo '</style>'."\n";
		echo '</head>'."\n";
		echo '<body>'."\n";
		echo '<table x:str="true" border="1" cellpadding="0" cellspacing="0">'."\n";
			echo '<tr>'."\n";
				echo '<th>Nom</th>'."\n";
				echo '<th>Jalon</th>'."\n";
				echo '<th>Date</th>'."\n";
			echo '</tr>'."\n";
			
		foreach($idle as $result):
			echo '<tr>'."\n";
			echo '<td>'.$result['name'].'</td>'."\n";
			echo '<td>'.$result['milestone'].'</td>'."\n";
			echo '<td>'.$result['deadline'].'</td>'."\n";
			echo '</tr>'."\n";
			
			$tickets = $this->Project->query('SELECT t.id, t.name, t.kind, t.status FROM tickets AS t WHERE t.batch_id='.$result['id']);
			
			echo '<tr><td></td>'."\n";
			echo '<td colspan="2">'."\n";
				echo '<table x:str="true" border="1" cellpadding="0" cellspacing="0">'."\n";
					echo '<tr>'."\n";
						echo '<th>Nom</th>'."\n";
						echo '<th>Type</th>'."\n";
						echo '<th>Etat</th>'."\n";
						echo '<th>Affectation</th>'."\n";
					echo '</tr>'."\n";
					
				foreach($tickets as $ticket):
				
				$users = $this->Project->query('SELECT u.firstname, u.lastname FROM ticket_users AS tu LEFT JOIN users AS u ON u.id = tu.user_id WHERE tu.ticket_id='.$ticket['t']['id']);
				
				$usr = "";
				for($i = 0; $i < sizeof($users); $i++){
					$usr .= $users[$i]['u']['firstname']." ".$users[$i]['u']['lastname'];
					if ($i < (sizeof($users) - 1))
						$usr .= "; ";
				}
				
					echo '<tr>'."\n";
						echo '<td>'.$ticket['t']['name'].'</td>'."\n";
						echo '<td>'.$ticket['t']['kind'].'</td>'."\n";
						echo '<td>'.$ticket['t']['status'].'</td>'."\n";
						echo '<td>'.$usr.'</td>'."\n";						
					echo '</tr>'."\n"; 
				endforeach;		
					
				echo '</table>'."\n";
			echo '</td></tr>'."\n";
			
		endforeach;	

		echo '</table>'."\n";
		echo '</body>'."\n";
		echo '</html>';
	}	
	
	/**
	 * Return the Rss for the "To qualify" element of the dashboard.
	 *
	 * @author Kévin Labesse
	 **/	
	public function rssQualTickets()
	{
		$qual = $this->Project->query("SELECT * FROM `tickets` WHERE `status` = 'to qualify' AND `drafted` = '0' AND `project_id` = ".$this->project_id." AND `description` <> '' ORDER BY creation_date ASC");
		
		echo '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<rss version="2.0">'."\n".'<channel>'."\n";
		
		echo '<title>'.$this->project_name.'</title>'."\n";
		echo '<link>http://www.smile.fr</link>'."\n";
		echo '<description>Tickets à qualifier</description>'."\n\n";
		
		foreach ($qual as $result)
		{	
			$titre = $result['tickets']['name'];
			$titre = stripslashes(trim($titre));
			$titre = str_replace("&nbsp;",'',$titre);
			$news = stripslashes(trim($result['tickets']['description']));
			$news = str_replace("&nbsp;",'',$news);
			$news = str_replace("<BR>",'<BR/>',$news);
			$news = str_replace("&",'&amp;',$news);
			$news = str_replace("<","&lt;",$news);
			$news = str_replace(">","&gt;",$news);
			$news = str_replace("", "e", $news);
			
			echo '<item>'."\n";
			echo '<title>'.$titre.'</title>'."\n";
			echo '<link>'.$this->base.'/'.$this->project_name.'/'.$this->project_id.'/tickets/view/'.$result['tickets']['id'].'</link>'."\n";
			echo '<description>';
			
			echo  $news; 
			
			echo '</description>'."\n".'</item>'."\n";
		}
		
		echo '</channel>'."\n".'</rss>';
		exit();		
	}

	/**
	 * Return the Xls for the "To qualify" element of the dashboard. (used on smile people board)
	 *
	 * @author Kévin Labesse
	 **/	
	public function xlsQualTickets()
	{
		$qual = $this->Project->query("SELECT * FROM `tickets` WHERE `status` = 'to qualify' AND `drafted` = '0' AND `project_id` = ".$this->project_id." AND `description` <> '' ORDER BY creation_date ASC");
		
		header("Content-Type: application/force-download");
		header("Content-Disposition: attachment; filename=\"webx_tickets.xls\"");

		echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"'."\n";
		echo 'xmlns:x="urn:schemas-microsoft-com:office:excel"'."\n";
		echo 'xmlns="www.w3.org/TR/REC-html40&...'."\n";
		echo '<head>'."\n";
			echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'."\n";
			echo '<meta name="ProgId" content="Excel.Sheet" />'."\n";
			echo '<meta name="Generator" content="Microsoft Excel 9" />'."\n";
			echo '<style>'."\n";
			echo '<!--//'."\n";
			echo 'td.xleuro {'."\n";
			echo 'mso-style-parent:style0;'."\n";
			echo 'mso-number-format:"\#\,\#\#0\.00\\ \0022\20AC\0022";'."\n";
			echo '}'."\n";
			echo '//-->'."\n";
			echo '</style>'."\n";
		echo '</head>'."\n";
		echo '<body>'."\n";
		echo '<table x:str="true" border="1" cellpadding="0" cellspacing="0">'."\n";
			echo '<tr>'."\n";
				echo '<th>Nom</th>'."\n";
				echo '<th>Description</th>'."\n";
				echo '<th>Date</th>'."\n";
			echo '</tr>'."\n";
		
		
			
		foreach($qual as $result):
			echo '<tr>'."\n";
			echo '<td>'.$result['tickets']['name'].'</td>'."\n";
			echo '<td>'.$result['tickets']['description'].'</td>'."\n";
			echo '<td>'.$result['tickets']['ideal_deliver_date'].'</td>'."\n";
			echo '</tr>'."\n";			
		endforeach;	

		echo '</table>'."\n";
		echo '</body>'."\n";
		echo '</html>';
	}
}
?>